package util;



import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;


/**
 * Created by Administrator on 2017/9/11.
 */
public class ObjUtil {


    private String name;
    private Integer age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    /**
     * 通过get set方法复制数据，
     * insertDate 中的get方法获取的数据为null时，
     * 如果dataBase中也有同样的get方法，且get方法的获取值不为null时，
     * 会调用insertDate 中get方法对应的set方法，将值set进insertDate
     *
     * @param insertDate
     * @param dataBase
     */

    public static void insertSelectiveGETSET(Object insertDate, Object dataBase) {

        if (insertDate == null || insertDate == null) {
            return;
        }


        Class clazz1 = insertDate.getClass();
        Class clazz2 = dataBase.getClass();

        Method[] methods = clazz1.getMethods();
        for (Method method : methods) {
            String getMethodName = method.getName();
            if (getMethodName.indexOf("get") == 0) {
                try {
                    Object value1 = method.invoke(insertDate);
                    if (value1 == null) {
                        Object value2 = clazz2.getMethod(getMethodName).invoke(dataBase);
                        if (value2 != null) {
                            String setMethodName = getMethodName.replaceFirst("get", "set");
                            clazz1.getMethod(setMethodName, value2.getClass()).invoke(insertDate, value2);
                        }
                    }
                } catch (Exception e) {

                    System.out.println(e.getMessage());
                }
            }
        }
    }


    public static void mergeObject(Object to, Object from, String[] excludeList) throws Exception {

        if (from == null || to == null) {
            throw new IllegalAccessException("'to' or 'from' is null");
        }

        if (to.getClass().equals(to.getClass())) {//类型相同
            Field[] fields = from.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                fields[i].setAccessible(true);
                Object value = fields[i].get(from);
                if (null != value) {
                    fields[i].set(to, value);
                }
                fields[i].setAccessible(false);
            }
        } else { //类型不同


        }


    }


    public static void main(String[] af) throws Exception {

        ObjUtil form = new ObjUtil();
        form.setName("afdsf");
        form.setBirthday(new Date());

        ObjUtil to = new ObjUtil() {
            private String a;

            public String getA() {
                return a;
            }

            public void setA(String a) {
                this.a = a;
            }
        };
        to.setName("to");
        to.setAge(1);
        ObjUtil.insertSelectiveGETSET(to, form);
        System.out.println(to.toString());
        ;


    }


    @Override
    public String toString() {
        return "ObjUtil{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}
